package projectx.util;

import java.text.Collator;
import java.text.SimpleDateFormat;
import java.util.Date;

public final class DateUtil {

	public static final String DATE_PATTERN = "yyyyMMdd";

	public static final String SHORT_DATE_PATTERN = "yyMMdd";

	public static final String DATE_VIEW_PATTERN = "yyyy-MM-dd";

	public static final String DATE_VIEW_PATTERN2 = "yyyy/MM/dd";

	public static String format() {

		return format(DATE_PATTERN, new Date());
	}

	public static String shortFormat() {

		return format(SHORT_DATE_PATTERN, new Date());
	}

	public static String format(Date date) {

		return format(DATE_PATTERN, date);
	}

	public static String format(String pattern, long time) {

		return format(pattern, new Date(time));
	}

	public static String format(String pattern, Date date) {

		if (date == null) {
			return "";
		}

		SimpleDateFormat formatter = new SimpleDateFormat(pattern);

		return formatter.format(date);
	}

	public static boolean isPastDate(Date date) {

		String today = format();
		String target = format(date);

		return (today.compareTo(target) >= 0) ? true : false;
	}

	public static boolean isFutureDate(Date date) {

		String today = format();
		String target = format(date);

		return (today.compareTo(target) <= 0) ? true : false;
	}

	public static Date parseDate(String dateStr) {
		SimpleDateFormat formatter = new SimpleDateFormat(DATE_PATTERN);
		Date date = null;
		try {
			date = formatter.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	public static java.sql.Date parseDateForSQL(String dateStr) {
		SimpleDateFormat formatter = new SimpleDateFormat(DATE_PATTERN);
		try {
			Date date = formatter.parse(dateStr);
			return new java.sql.Date(date.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String formatDate(String dateSource, String pattern,
			String newPattern) {

		SimpleDateFormat formatFrom = new SimpleDateFormat(pattern);
		SimpleDateFormat formatTo = new SimpleDateFormat(newPattern);

		try {
			Date date = formatFrom.parse(dateSource);
			return formatTo.format(date);
		} catch (Exception e) {
			return dateSource;
		}
	}

	public static String formatDate(String dateSource, String newPattern) {

		if (!StringUtil.isBlank(dateSource)) {

			String newStr = dateSource;

			if (dateSource.indexOf("-") >= 0) {
				newStr = StringUtil.replaceAll(newStr, "-", "");
			}
			if (dateSource.indexOf("/") >= 0) {
				newStr = StringUtil.replaceAll(newStr, "/", "");
			}
			if (dateSource.indexOf(" ") >= 0) {
				newStr = StringUtil.replaceAll(newStr, " ", "");
			}

			if (newStr.length() == 8) {
				return formatDate(newStr, DATE_PATTERN, newPattern);
			}
		}

		return dateSource;
	}

	public static String formatDate(Date date, String newPattern) {
		SimpleDateFormat formatTo = new SimpleDateFormat(newPattern);

		try {

			return formatTo.format(date);

		} catch (Exception e) {
			return "";
		}
	}

	public static String format(String pattern) {

		return format(pattern, new Date());
	}

	public static boolean compareDate(String date1, String date2) {

		if (StringUtil.isBlank(date1) && StringUtil.isBlank(date2)) {
			return true;
		} else if (StringUtil.isBlank(date1)) {
			return false;
		} else if (StringUtil.isBlank(date2)) {
			return false;
		}

		if (date1.length() != date2.length()) {
			date1 = formatDate(date1, DateUtil.DATE_PATTERN);
			date2 = formatDate(date2, DateUtil.DATE_PATTERN);
		}

		Collator myCollator = Collator.getInstance();
		return (myCollator.compare(date1, date2) == 0);

	}

	private DateUtil() {
		super();
	}
}